package com.android.questions.util;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import cn.tongdun.android.shell.FMAgent;
import cn.tongdun.android.shell.inter.FMCallback;

/**
 * FileName: FMAgentManger
 * Author: admin
 * Date: 2022/1/25 14:32
 * Description:
 */
public class FMAgentManger {
    public static String tdBlackbox = "";
    public static boolean needCallback = true;

    public static void initWithCallback(Context context, Callback callback) {
        // FMAgent.ENV_SANDBOX     表示沙盒环境
        // FMAgent.ENV_PRODUCTION  表示生产环境
        FMAgent.initWithCallback(context, FMAgent.ENV_PRODUCTION, new FMCallback() {
            @Override
            public void onEvent(String s) {
                tdBlackbox = s;
                Log.e("FMAgent", "blackbox:" + param(tdBlackbox));
                if (callback != null && needCallback) {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onEvent(param(tdBlackbox));
                        }
                    });
                }
            }
        });
    }

    public static String param(String blackbox) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bb", blackbox);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }

    public interface Callback {
        void onEvent(String s);
    }
}
